﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Common;
using CNCSoft.EData;
using System.Data.SqlClient;

using DevExpress.XtraGrid.Views.Grid;


namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class frmKhoaHoc_GiaoVien : Form
    {

        DataSet ds = new DataSet();
        DataTable DM_KhoaHoc_Ready;

        public frmKhoaHoc_GiaoVien()
        {
            InitializeComponent();
        }

        private void frmKhoaHoc_GiaoVien_Load(object sender, EventArgs e)
        {
            ds.Tables.Add("KHOAHOC_GIAOVIEN");

            //So lieu DM cho may Combo
            try
            {
                DM_KhoaHoc_Ready = new DataTable();
                SQLHelper.Fill(DM_KhoaHoc_Ready, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_KHOAHOC WHERE TrangThai = 'Ready'");
                cboKhoaHoc.Properties.DataSource = DM_KhoaHoc_Ready;
                cboKhoaHoc.Properties.DisplayMember = "TenKhoaHoc";
                cboKhoaHoc.Properties.ValueMember = "MaKhoaHoc";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Ham nay dung de dua danh sach giao vien vao lop
        private void GetData_DSLop(string MaLop)
        {
            try
            {
                if (ds.Tables["KHOAHOC_GIAOVIEN"] != null)
                    ds.Tables["KHOAHOC_GIAOVIEN"].Clear();
                SQLHelper.Fill(ds.Tables["KHOAHOC_GIAOVIEN"], GlobalVariable.WSInfo.ConnectionString,
                                                        "SELECT ID, MaKhoaHoc, MaGiaoVien, RateUSD, RateVND, DangDay, Note " +
                                                        "FROM KhoaHoc_GiaoVien " +
                                                        "WHERE MaKhoaHoc = '" + MaLop + "'");

                gridKhoaHoc_GiaoVien.DataSource = ds;
                gridKhoaHoc_GiaoVien.DataMember = "KHOAHOC_GIAOVIEN";

            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }
        }


        //Event nay dung de load danh sach Giao vien can chon de them vao giang day cho lop
        private void gridViewDataInAvailableClass_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            try
            {
                //Load Danh sach hoc sinh da enroll nhung chua hoc lop nao (de chon bo sung)
                frmView fv = new frmView("SELECT  MaGiaoVien, TenGiaoVien, TenVietTat, MaQuocTich " +
                                         "FROM DM_GIAOVIEN " +
                                         "WHERE MaDonVi = 'English' ", "");
                fv.ShowDialog(this);

                if (fv.FlChoose)  //Da chon GiaoVien
                {
                    //Gan cac gia tri tu form da chon vao datatable "DS_HOCSINH"  (di nhien la thong qua grid)
                    DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;

                    //Gan gia tri cho field MaGiaoVien
                    string MaGiaoVien = fv.SelectedIDs.Replace("('", "").Replace("')", "").ToString();
                    view.SetRowCellValue(e.RowHandle, view.Columns["MaGiaoVien"], MaGiaoVien);

                    //Gan gia tri cho field MaKhoaHoc
                    view.SetRowCellValue(e.RowHandle, view.Columns["MaKhoaHoc"], cboKhoaHoc.EditValue.ToString());
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }

        }

        private void cboKhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            //Load DS Giao vien da dang ly cho lop
            GetData_DSLop(cboKhoaHoc.EditValue.ToString());
        }

        private void cmdCapNhat_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

            //Chua bi cau lenh
            string sqlInsert, sqlUpdate, sqlDelete;
            sqlInsert = "INSERT INTO KHOAHOC_GIAOVIEN(MaKhoaHoc, MaGiaoVien, RateUSD, RateVND, DangDay, Note) " +
                                              " VALUES(@pMaKhoaHoc, @pMaGiaoVien, @pRateUSD, @pRateVND, @pDangDay, @pNote )";

            sqlUpdate = "UPDATE KHOAHOC_GIAOVIEN SET MaKhoaHoc = @pMaKhoaHoc, MaGiaoVien = @pMaGiaoVien, RateUSD = @pRateUSD, RateVND = @pRateVND, DangDay = @pDangDay, Note = @pNote " +
                        " WHERE ID = @pID";

            sqlDelete = "DELETE FROM KHOAHOC_GIAOVIEN WHERE ID = @pID";

            //Chuan bi tham so
            SqlParameter[] pInsert = new SqlParameter[6];
            SqlParameter[] pUpdate = new SqlParameter[7];
            SqlParameter[] pDelete = new SqlParameter[1];

            pInsert[0] = new SqlParameter("@pMaKhoaHoc", SqlDbType.VarChar, 20, "MaKhoaHoc");
            pInsert[1] = new SqlParameter("@pMaGiaoVien", SqlDbType.VarChar, 20, "MaGiaoVien");
            pInsert[2] = new SqlParameter("@pRateUSD", SqlDbType.Float, 18, "RateUSD");
            pInsert[3] = new SqlParameter("@pRateVND", SqlDbType.Float, 18, "RateVND");
            pInsert[4] = new SqlParameter("@pDangDay", SqlDbType.Bit, 1, "DangDay");
            pInsert[5] = new SqlParameter("@pNote", SqlDbType.NVarChar, 200, "Note");

            pUpdate[0] = new SqlParameter("@pMaKhoaHoc", SqlDbType.VarChar, 20, "MaKhoaHoc");
            pUpdate[1] = new SqlParameter("@pMaGiaoVien", SqlDbType.VarChar, 20, "MaGiaoVien");
            pUpdate[2] = new SqlParameter("@pRateUSD", SqlDbType.Float, 18, "RateUSD");
            pUpdate[3] = new SqlParameter("@pRateVND", SqlDbType.Float, 18, "RateVND");
            pUpdate[4] = new SqlParameter("@pDangDay", SqlDbType.Bit, 1, "DangDay");
            pUpdate[5] = new SqlParameter("@pNote", SqlDbType.NVarChar, 200, "Note");
            pUpdate[6] = new SqlParameter("@pID", SqlDbType.Int, 18, "ID");

            pDelete[0] = new SqlParameter("@pID", SqlDbType.Int, 18, "ID");

            SqlCommand cmdInsert = new SqlCommand(sqlInsert, cnn);
            SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);
            SqlCommand cmdDelete = new SqlCommand(sqlDelete, cnn);

            cmdInsert.Parameters.AddRange(pInsert);
            cmdUpdate.Parameters.AddRange(pUpdate);
            cmdDelete.Parameters.AddRange(pDelete);

            SqlDataAdapter da = new SqlDataAdapter();
            da.InsertCommand = cmdInsert;
            da.UpdateCommand = cmdUpdate;
            da.DeleteCommand = cmdDelete;

            //Update
            try
            {
                da.Update(ds, "KHOAHOC_GIAOVIEN");
                ds.AcceptChanges();
                MessageBox.Show("Cập nhật thành công danh giáo viên giảng dạy lớp " + cboKhoaHoc.EditValue.ToString(), "Cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdBoQua_Click(object sender, EventArgs e)
        {
            //Load lai so lieu
            GetData_DSLop(cboKhoaHoc.EditValue.ToString());
        }


    }
}
